﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System;
using System.Collections.Generic;
using System.Data.Common;
using NetBay.Core.BusinessEntities;
using NetBay.Core.Extensions;
using NetBay.Core.Log;
using System.Data;
#endregion

namespace Rio.Doc.Business.Repository.Translator
{
    /// <summary>
    /// Class used to convert Group database row to Group object
    /// </summary>
    public class GeneralGroupTranslator
    {
        /// <summary>
        /// Translate a bunch of Group DataReader to Group object
        /// </summary>
        /// <param name="P_oDbDataReader">The dataReader that contains the rows</param>
        /// <returns>A list of Group Object</returns>
        public static List<Group> TranslateGroupWithRights(IDataReader P_oDbDataReader)
        {
            //Variable initialization.
            var l_lstGroups = new List<Group>();

            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("GeneralGroupTranslator.TranslateGroupWithRights (P_oDbDataReader <" + P_oDbDataReader.ToString() + ">)...");

            try
            {
                using (P_oDbDataReader)
                {
                    while (P_oDbDataReader.Read())
                    {
                        Group l_oGroup = new GeneralGroupRepository();

                        l_oGroup.Identifier = Convert.ToInt32(P_oDbDataReader["RG_ID"].ToString());
                        l_oGroup.Name = P_oDbDataReader["RG_NOM"].ToString();
                        l_oGroup.CanConsult =
                            Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RG_CONS"].ToString()));
                        l_oGroup.CanReorganize =
                            Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RG_REORG"].ToString()));
                        l_oGroup.IsAdministrator =
                            Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RG_ADMIN"].ToString()));
                        l_oGroup.CanScann =
                            Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RG_NUM"].ToString()));
                        l_oGroup.CanPrint =
                            Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RG_IMPRESSION"].ToString()));
                        l_oGroup.CanDownload =
                            Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RG_TELECHARGER"].ToString()));
                        l_oGroup.CanSendEmail =
                            Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RG_EMAIL"].ToString()));


                        if (P_oDbDataReader["RG_FLOW"] != DBNull.Value)
                        {
                            l_oGroup.CanFlow =
                                Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RG_FLOW"].ToString()));
                        }
                        else
                        {
                            l_oGroup.CanFlow = true;
                        }

                        if (P_oDbDataReader["RG_MASSIMPORT"] != DBNull.Value)
                        {
                            l_oGroup.CanMassImport =
                                Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RG_MASSIMPORT"].ToString()));
                        }
                        else
                        {
                            l_oGroup.CanMassImport = false;
                        }

                        try
                        {
                            l_oGroup.CanDelete =
                                   Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RG_DELETE"].ToString()));
                        }
                        catch (Exception)
                        {
                            //Old compatibility
                            l_oGroup.CanDelete = l_oGroup.CanReorganize;
                        }

                        try
                        {
                            l_oGroup.CanFlowValidation =
                                   Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RG_FLOWVALID"].ToString()));
                        }
                        catch (Exception)
                        {
                            l_oGroup.CanFlowValidation = false;
                        }

                        //Trash item right
                        try
                        {
                            l_oGroup.CanTrash =
                                   Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["RG_REMOVE"].ToString()));
                        }
                        catch (Exception)
                        {
                            l_oGroup.CanTrash = false;
                        }

                        try
                        {
                            if (P_oDbDataReader["IS_USER"] != null &&
                                String.IsNullOrEmpty(P_oDbDataReader["IS_USER"].ToString()))
                            {
                                l_oGroup.IsUser =
                                    Convert.ToBoolean(Convert.ToInt16(P_oDbDataReader["IS_USER"].ToString()));
                            }
                        }
                        catch (Exception)
                        {
                            l_oGroup.IsUser = false;
                        }

                        l_lstGroups.Add(l_oGroup);
                    }
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return l_lstGroups;
        }


        /// <summary>
        /// Translates the group.
        /// </summary>
        /// <param name="DbDataReader">The db data reader.</param>
        /// <returns></returns>
        public static List<Group> TranslateGroupWithoutRights(IDataReader DbDataReader)
        {
            //Variable initialization
            var l_lstGroups = new List<Group>();

            //Log.
            NetBay.Core.Log.LogManager.Instance().LogDebug("GeneralGroupTranslator.TranslateGroupWithRights (DbDataReader <" + DbDataReader.ToString() + ">)...");

            try
            {
                using (DbDataReader)
                {
                    while (DbDataReader.Read())
                    {
                        Group l_oGroup = new GeneralGroupRepository();

                        l_oGroup.Identifier = DbDataReader.GetLongValue("RG_ID");
                        l_oGroup.Name = DbDataReader["RG_NOM"].ToString();
                        l_oGroup.Description = DbDataReader.GetStringValue("RG_DESCRIPTION");
                        try
                        {
                            l_oGroup.IsUser =
                                DbDataReader.GetBooleanValue("IS_USER");
                        }
                        catch (Exception)
                        {
                            l_oGroup.IsUser = false;
                        }

                        l_lstGroups.Add(l_oGroup);
                    }
                }
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return l_lstGroups;
        }
    } 
}
